<template>
    <div class='approval'>
        <el-card class="box-card" style="margin-top: 110px;">
            <div class='title'>
                {{$t('approvalDispatch.title1')}}
            </div>
            <div class='menu-search'> 
                <div class='menu-title'>
                    <!-- 业务ID -->
                    <div class='searchItem'>
                        <span>{{$t('approvalDispatch.orderId')}}</span>
                        <el-input v-model='listQuery.orderId' :placeholder="$t('rule.orderId')" size='small'></el-input>
                    </div>
                    <!-- 客户姓名 -->
                    <div class='searchItem'>
                        <span>{{$t('approvalDispatch.realName')}}</span>
                        <el-input v-model='listQuery.realName' :placeholder="$t('rule.realName')" size='small'></el-input>
                    </div>
                    <!-- 手机号 -->
                    <div class='searchItem'>
                        <span>{{$t('approvalDispatch.phoneNo')}}</span>
                        <el-input v-model='listQuery.phoneNo' :placeholder="$t('rule.phoneNo')" size='small'></el-input>
                    </div>
                    <!-- 领取人 -->
                    <div class='searchItem'>
                        <span>{{$t('approvalDispatch.receiver')}}</span>
                        <el-input v-model='listQuery.checkUser' :placeholder="$t('rule.receiver')" size='small'></el-input>
                    </div>
                    <!-- 任务状态 -->
                    <div class='searchItem'>
                      <span>{{$t('processing.taskStatus')}}</span>
                      <el-select v-model='listQuery.dellStatus' size='small'>
                          <el-option
                          v-for='item in dellStatusArr'
                          :key='item.value'
                          :label='item.label'
                          :value='item.value'>
                          </el-option>
                      </el-select>
                  </div>
                  <!-- 是否续贷 -->
                  <div class='searchItem'>
                    <span>{{$t('orderQuery.ctnLoan')}}</span>
                    <el-select v-model='listQuery.isContinued' size='small'>
                      <el-option
                      v-for='item in conLoan'
                      :key='item.value'
                      :label='item.label'
                      :value='item.value'>
                      </el-option>
                    </el-select>
                  </div>
                    <!-- 申请时间 -->
                    <div class='searchItem searchItem-date'>
                        <span>{{$t('approvalDispatch.crtTime')}}</span>
                        <el-date-picker
                        size='small'
                        v-model="listQuery.beginDate"
                        type="date"
                        value-format="yyyy-MM-dd"
                        :placeholder="$t('rule.beginDate')">
                        </el-date-picker>
                        <b>{{$t('approval.go')}}</b>
                        <el-date-picker
                        size='small'
                          v-model="listQuery.endDate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          :placeholder="$t('rule.endDate')">
                        </el-date-picker>
                    </div>
                </div>
                <div class='menu-title-btn'>
                  <button type='button' @click='search'>
                      {{$t('btn.query')}}
                  </button>
                  <button type='button' @click='reset'>
                      {{$t('btn.reset')}}
                  </button>
              </div>
            </div>
        </el-card>
        <el-card class='box-card' style="margin-top: 10px;margin-bottom: 20px;">
            <div class='title'>
                {{$t('approvalDispatch.title2')}}
            </div>
            <div class='menu-list'>
              <div>
                <el-button type="text" @click="assignedTo">{{$t('approvalDispatch.assignTo')}}</el-button>
              </div>
                <div style="display: flex; margin-left: 30px;">
                  <!-- 已领取 -->
                  <div class="type-marker">
                    <span class="bg color2"></span>
                    <span style="font-size: 14px;">{{$t('dispatch.receive')}}</span>
                  </div>
                  <!-- 未领取 -->
                  <div class="type-marker">
                    <span class="bg color1"></span>
                    <span style="font-size: 14px;">{{$t('dispatch.unreceived')}}</span>
                  </div>
                </div>
            </div>
            <el-table
                :row-class-name="dispatchClass"
                v-loading="listLoading"
                ref='multipleTable'
                :data='tableData'
                tooltip-effect='dark'
                style='width: 100%'
                @selection-change='handleSelectionChange'
                border
                size='mini'>
                <el-table-column
                header-align='center'
                align='center'
                type='selection'
                width='55'>
                </el-table-column>
                <!-- 业务ID -->
                <el-table-column
                prop='orderId' align="center" header-align="center" 
                :label="$t('approvalDispatch.orderId')"
                width='140'>
                </el-table-column>
                <!-- 客户姓名 -->
                <el-table-column
                prop='realName' align="center" header-align="center" 
                :label="$t('approvalDispatch.realName')"
                width='180'>
                </el-table-column>
                <!-- 手机号 -->
                <el-table-column
                prop='appUserName' align="center" header-align="center" 
                :label="$t('approvalDispatch.phoneNo')"
                width='180'>
                </el-table-column>
                <!-- 产品名称 -->
                <el-table-column
                prop='proName' align="center" header-align="center" 
                :label="$t('approvalDispatch.proName')"
                width='140'>
                </el-table-column>
                <!-- 申请金额 -->
                <el-table-column
                prop='applyAmt' align="center" header-align="center" 
                :label="$t('approvalDispatch.applyAmt')"
                width='100'>
                </el-table-column>
                <!-- 申请时间 -->
                <el-table-column
                prop="crtTime" align="center" header-align="center" 
                :label="$t('approvalDispatch.crtTime')"
                width='200'>
                </el-table-column>
                <!-- 领取人 -->
                <el-table-column
                prop='businessUserName' align="center" header-align="center" 
                :label="$t('approvalDispatch.receiver')"
                width='200'>
                </el-table-column>
                <!-- 是否续贷 -->
                <el-table-column
                prop='attr5' align="center" header-align="center" 
                :label="$t('orderQuery.ctnLoan')"
                width='200'>
                  <template slot-scope="scope">
                    <span v-if="scope.row.attr5 === '1'">{{$t('approvalDetail.yes')}}</span>
                    <span v-if="scope.row.attr5 !== '1'">{{$t('approvalDetail.no')}}</span>
                  </template>
                </el-table-column>
                <!-- 业务渠道 -->
                <el-table-column
                prop='appShortName' align="center" header-align="center" 
                :label="$t('approvalDispatch.appShortName')"
                width='140'>
                </el-table-column>
                <!-- 审批意见 -->
                <el-table-column
                prop='checkOpinion' align="center" header-align="center" 
                :label="$t('approvalDispatch.checkOpinion')"
                width='300'
                show-overflow-tooltip>
                </el-table-column>
            </el-table>
            <div class="pagination-container">
              <el-pagination
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="listQuery.page"
              :page-sizes="[5, 10, 20, 30]"
              :page-size="listQuery.limit"
              layout="total, sizes, prev, pager, next, jumper"
              :total="total">
              </el-pagination>
            </div>
        </el-card>
        <el-dialog :title="$t('approvalDispatch.chooseTo')" :visible.sync="dialogFormVisible">
            <!-- 处理人 -->
            <div class='handler'>
                <span>{{$t('approvalDispatch.handler')}}</span>
                <el-select v-model='value2' :placeholder="$t('rule.choose')" size='small'>
                    <el-option
                    v-for='(item, index) in region'
                    :key='index'
                    :label='item.username'
                    :value='item.id'>
                    </el-option>
                </el-select>
            </div>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">{{$t('btn.cancel')}}</el-button>
                <el-button type="primary" @click="confirmFn">{{$t('btn.confirm')}}</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { mapGetters } from "vuex";
import { page, getAllBaseUser, loanDispatch } from 'api/trust/approvalDispatch/index'
export default {
  name: "approvalDispatch",
  data() {
    return {
      listLoading: false,
      total: null,
      listQuery: {
        limit: 5,
        page: 1,
        orgId: "",//业务ID
        realName: "",
        phoneNo: "",
        checkUser: "",//签收人
        beginDate: "",
        endDate: "",
        dellStatus: "",
        isContinued: 0
      },
      tableData: [],
      multipleSelection: [],
      value2: "",
      dialogFormVisible: false,
      region: [],
      dellStatusArr: [
        {
          value: '',
          label: this.$t('rule.all')
        },
        {
          value: 1,
          label: this.$t('dispatch.receive')
        },
        {
          value: 2,
          label: this.$t('dispatch.unreceived')
        }
      ],
      conLoan: [
        {
          value: 0,
          label: this.$t('rule.all')
        },
        {
          value: 1,
          label: this.$t('approvalDetail.yes')
        },
        {
          value: 2,
          label: this.$t('approvalDetail.no')
        }
      ]
    };
  },
  computed: {
    ...mapGetters(["elements", "apprDispPage"])
  },
  created() {
    if (this.apprDispPage.has(this.$route.path)) {
      this.listQuery = this.apprDispPage.get(this.$route.path)
    }
    this.getList();
  },
  methods: {
    // 标记颜色
    dispatchClass(row) {
      if (row.row.checkUserId === '') {
        return 'unassign-row';
      } else {
        return 'allocated-row';
      }
    },
    getList() {
      this.listLoading = true;
      this.$store.dispatch('saveApprDispPage', { path: this.$route.path, listQuery: this.listQuery });
      page(this.listQuery)
        .then(response => {
          response.data.rows.forEach(item => {
            let re = /(?=(?!\b)(\d{3})+$)/g;
            let str = String(item.applyAmt)
            item.applyAmt =  str.replace(re, ',');
          })
          if (response.status === 200) {
            this.listLoading = false;
            this.tableData = response.data.rows;
            this.total = Number(response.data.total);
          }
        })
    },
    //查询
    search() {
      this.listQuery.page = 1;
      this.getList();
    },
    //重置
    reset() {
      this.listQuery.limit = 5;
      this.listQuery.page = 1;
      this.listQuery.orderId = "";
      this.listQuery.realName = "";
      this.listQuery.phoneNo = "";
      this.listQuery.checkUser = "";
      this.listQuery.beginDate = "";
      this.listQuery.endDate = "";
      this.listQuery.dellStatus = "";
      this.listQuery.isContinued = 0;
    },
    //选中的数据
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    // 指定代理人
    assignedTo() {
      if (this.multipleSelection.length <= 0){
        this.$message({
          showClose: true,
          message: this.$t('approvalDispatch.msg1'),
          type: 'warning'
        });
      } else {
        let obj = {};
        obj.channel = this.multipleSelection[0].channel;
        obj.code = 'approve';
        getAllBaseUser(obj)
        .then(response => {
          this.region = response.data.userList;
        })
        this.dialogFormVisible = true
      }
    },
    //指定处理人确认按钮
    confirmFn() {
      if (this.value2 !== '') {
        let obj = {};
        let arrId = [];
        this.multipleSelection.forEach(item => {
          arrId.push(item.orderId)
        })
        obj.orderId = arrId.join();
        obj.baseUserId = this.value2;
        loanDispatch(obj)
        .then(response => {
          if (response.status === 200) {
            this.$message({
              showClose: true,
              message: this.$t('approvalDispatch.opersuccess'),
              type: 'success'
            });
            this.dialogFormVisible = false
            this.value2 = ''
            this.getList()
          }
        })
      } else {
        this.$message({
          showClose: true,
          message: this.$t('rule.pleAssignTo'),
          type: 'warning'
        });
      }
    },
    handleSizeChange(val) {
      this.listQuery.limit = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.page = val;
      this.getList();
    }
  }
};
</script>

<style scoped lang='scss'>
.box-card {
  width: 95%;
  margin-left: 2.5%;
  margin-right: 2.5%;
}
.title {
  line-height: 30px;
  font-size: 16px;
  display: flex;
  align-items: center;
  font-weight: bold;
}
.title:before{
  display: block;
  content: "";
  width: 3px;
  height: 16px;
  background-color: #309afe;
}
.title:before {
  margin-right: 8px;
}
::before {
  box-sizing: inherit;
  -webkit-tap-highlight-color: transparent;
}
.menu-title {
  width: 100%;
  box-sizing: border-box;
  background: #ffffff;
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  .searchItem {
    display: flex;
    align-items: center;
    padding: 10px  0;
    span {
      display: inline-block;
      width: 100px;
      text-align: right;
      font-size: 12px;
      color: #5f5f61;
      padding-right: 5px;
    }
    div:nth-child(2) {
      width: 150px;
    }
  }
  .searchItem-date {
    display: flex;
    align-items: center;
    padding: 10px  0;
    b {
      margin: 0 10px;
      font-weight: normal;
      font-size: 12px;
      color: #5f5f61;
    }
    div {
       width: 150px;
    }
  }
}
.menu-title-btn {
  padding: 10px 0;
  box-sizing: border-box;
  button {
    padding: 0 20px;
    height: 28px;
    background: #20a0ff;
    color: #fff;
    outline: none;
    border-width: 0;
    border-radius: 4px;
    font-size: 12px;
    margin-left: 10px;
    &:nth-child(1) {
      margin-left: 0;
    }
    &:nth-child(2) {
      background: #ebf5ff;
      color: #20a0ff;
      border: 1px solid #abd5ff;
    }
  }
}
.menu-list {
  padding: 20px 0;
  box-sizing: border-box;
  button {
    padding: 0 20px;
    height: 28px;
    background: #20a0ff;
    color: #fff;
    outline: none;
    border-width: 0;
    border-radius: 4px;
    font-size: 12px;
    margin-left: 10px;
    &:nth-child(1) {
      margin-left: 0;
    }
  }
  button[disabled] {
    padding: 0 20px;
    height: 28px;
    background: #c1e0ff;
    color: #fafbff;
    outline: none;
    border-width: 0;
    border-radius: 4px;
    font-size: 12px;
  }
}
.handler {
  width: 90%;
  display: flex;
  justify-content: space-between;
  align-items: center;
    span {
      display: inline-block;
      width: 80px;
      text-align: center;
      font-size: 14px;
      font-weight: bold;
    }
    div:nth-child(2) {
      flex: 1;
    }
}
.menu-list {
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.bg {
  display: inline-block;
  border-radius: 5px;
  border: 2px solid #fff;
  width: 25px;
  height: 25px;
  cursor: pointer;
}
.color1 {
  background: #ec7070;
}
.color2 {
  background: #73d466;
}
.type-marker {
  display: flex;
  align-items: center;
  margin-right: 20px;
}
</style>